package com.zhuannimei.ui;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.util.LogUtils;
import com.zhuannimei.R;
import com.zhuannimei.net.RequestServerData;
import com.zhuannimei.utility.JsonUtil;
import com.zhuannimei.utility.UserData;
import com.zhuannimei.utility.WuLog;
import com.zhuannimei.utility.ZnmData;
import com.zhuannimei.utility.ZnmHttpParams;

/**
 * 成为推广员
 * 
 * @author Jack 2013-12-17
 * 
 * 
 */
public class JoinUsActivity extends BaseActivity {

	private static final String TAG = "JoinUsActivity";

	private EditText name_et;
	private EditText phone_et;
	private EditText address_et;
	private EditText qq_et;
	private EditText position_et;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.join_us);
		// 启动activity时不自动弹出软键盘
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		initView();
	}

	@Override
	protected void onStart() {
		super.onStart();
		mTitlePane.getLeftButton().setVisibility(View.VISIBLE);
		mTitlePane.getRightButton().setVisibility(View.VISIBLE);
		mTitlePane.setTitle("成为推广员");
		mTitlePane.setLeftButtonTextAndListener("返回", new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mTitlePane.setRightButtonTextAndListener("提交", new OnClickListener() {

			@Override
			public void onClick(View v) {
				joinUs();
			}
		});
	}

	private void initView() {
		name_et = (EditText) findViewById(R.id.name_et);
		phone_et = (EditText) findViewById(R.id.phone_et);
		address_et = (EditText) findViewById(R.id.address_et);
		qq_et = (EditText) findViewById(R.id.qq_et);
		position_et = (EditText) findViewById(R.id.position_et);
	}

	private boolean isSend() {
		if (TextUtils.isEmpty(name_et.getText().toString())) {
			showCustomToast("真实姓名不能为空");
			return false;
		}
		WuLog.i(TAG, name_et.toString() + ";;" + phone_et.toString());
		if (TextUtils.isEmpty(phone_et.getText().toString())) {
			showCustomToast("手机号不能为空");
			return false;
		}
		if (TextUtils.isEmpty(address_et.getText().toString())) {
			showCustomToast("联系地址不能为空");
			return false;
		}

		if (TextUtils.isEmpty(qq_et.getText().toString())) {
			showCustomToast("qq号不能为空");
			return false;
		}
		if (TextUtils.isEmpty(position_et.getText().toString())) {
			showCustomToast("职业不能为空");
			return false;
		}
		return true;
	}

	private void joinUs() {
		if (isSend()) {
			RequestParams mParams = ZnmHttpParams.getDefaultRequestParams();
			mParams.addBodyParameter("userId", ""
					+ ZnmData.getUserData().getUserUserId());
			mParams.addBodyParameter("realname", name_et.getText().toString());
			mParams.addBodyParameter("mobile", phone_et.getText().toString());
			mParams.addBodyParameter("address", address_et.getText().toString());
			mParams.addBodyParameter("qq", qq_et.getText().toString());
			mParams.addBodyParameter("job", position_et.getText().toString());
			RequestServerData.getJoinUs(mParams, new RequestCallBack<String>() {
				@Override
				public void onSuccess(ResponseInfo<String> responseInfo) {
					try {
						JSONObject jo = new JSONObject(responseInfo.result);
						if ("0".equals(jo.optString("code"))) {
							showCustomToast("提交成功，请等待审核");
						}
						finish();
					} catch (JSONException e) {
						e.printStackTrace();
						showCustomToast("网络不给力");
					}
				}

				@Override
				public void onFailure(HttpException error, String msg) {
					showCustomToast("网络不给力");
				}
			});
		}
	}
}
